package com.zx.framework.core.util.layer;

import com.zx.framework.core.util.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 使用36进制实现菜单、部门、角色等唯一layer的生成 ZZZ=45361
 * @author stylewolf
 *
 */
public class LayerUtil {
	/**
	 * layer的长度以3为分割
	 */
	public static final int LENGTH = 3;
	/**
	 * 前缀的分隔符 zxkj-001
	 */
	public static final String SPLIT = "-";
	/**
	 *每个layer的最大值
	 */
	public static final String MAX = "zzz";
	
	
	public static final int RADIX = 36;
	
	public static final String getPrefix(String layer){
		int pos = layer.indexOf(SPLIT);
		return layer.substring(0,pos);
	}
	
	public static final String getSuffix(String layer){
		int pos = layer.indexOf(SPLIT);
		return layer.substring(pos+1);
	}
	/**
	 * 获取layer描述对象
	 * @param layer
	 * @return
	 */
	public static final LayerModle getLayerModle(String layer,String parentId){
		LayerModle model = new LayerModle();
		if (StringUtil.isEmpty(layer)&&parentId.indexOf(SPLIT)!=-1){
			layer = parentId+"000";
		}
		if (StringUtil.isEmpty(layer)&&parentId.indexOf(SPLIT)==-1){
			layer = parentId+"-000";
		}
		layer = layer.toLowerCase();
		String[] array = layer.split(SPLIT);
		if(array.length == 1){
			model.getGroup().add(array[0]);
			return model;
		}
		
		String suffix = array[1];
		model.setPrefix(array[0]);
		model.setSuffix(suffix);
		String temp = "";
		for (int i = 0; i < suffix.length(); i++) {
			temp+="z";
		}
		Pattern pattern = Pattern.compile("([\\w]{3})");
		Matcher matcher = pattern.matcher(suffix);
		
		while (matcher.find()) {
			model.getGroup().add(matcher.group(0));
		}
		model.setMax(temp);
		return model;
	}
	
	public static void main(String[] args) {
		String zxkj = "sgz";
		LayerModle model = getLayerModle(zxkj,null);
//		System.out.println("prefix:"+getPrefix(zxkj));
//		System.out.println("suffix:"+getSuffix(zxkj));
//		
//		System.out.println("prefix:"+model.getPrefix());
//		System.out.println("suffix:"+model.getSuffix());
//		System.out.println("max:"+model.getMax());
		System.out.println("next:"+model.getNext());
		
//		String zxkj1 = "zxkj1-0010020zz";
//		LayerModle model1 = getLayerModle(zxkj1);
//		System.out.println("prefix:"+getPrefix(zxkj));
//		System.out.println("suffix:"+getSuffix(zxkj));
//		
//		System.out.println("prefix:"+model.getPrefix());
//		System.out.println("suffix:"+model.getSuffix());
//		System.out.println("max:"+model.getMax());
//		System.out.println("next:"+model1.getNext());
	}
}
